package com.smalls.newvideotwo.mvp.presenter;

import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.mvp.base.BaseBeanResult;
import com.smalls.newvideotwo.mvp.base.BasePresenter;
import com.smalls.newvideotwo.mvp.beanResult.GetChannelsResult;
import com.smalls.newvideotwo.mvp.http.constants.PortalErr;
import com.smalls.newvideotwo.mvp.http.constants.PortalErrUtil;
import com.smalls.newvideotwo.mvp.model.GetChannelModel;
import com.smalls.newvideotwo.mvp.view.IGetChannelView;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;

import org.xutils.ex.HttpException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeoutException;

import static com.smalls.newvideotwo.mvp.http.constants.PortalErr.*;

/**
 * Created by cindy on 2018/4/15.
 */

public class GetChannelPresenter extends BasePresenter<IGetChannelView> implements BaseBeanResult.IGetChannel {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "GetChannelPresenter":"GetChannelPresenter";
    private boolean isLive;
    private int categoryLevel = 1;
    private com.smalls.newvideotwo.mvp.model.modelInterface.IGetChannel model = new GetChannelModel();
    private int start,end;

    public GetChannelPresenter(IGetChannelView view) {
        super(view);
    }

    public void requestData(boolean isLive,int start,int end) {
        LogUtil.d(this.TAG, "requestData");
        this.isLive = isLive;
        this.start = start;
        this.end = end;
        model.getChannel(this,start,end);
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        String errCode = "";
        if ((e instanceof TimeoutException) || (e instanceof ConnectException) || (e instanceof SocketTimeoutException)) {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB2");
            NewDTVApp.getApp().setmEpgServerNext();
            if (!NewDTVApp.getApp().isLastServer()) {
                model.getChannel(this,start,end);
                return;
            }
            errCode = STR_BOX_EPG_TIMEOUT_ERROR;
        } else {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB3");
            errCode = PORTAL_SYSTEM;
        }
        PortalErrUtil.getInstance().setErrCode(errCode);
        ((IGetChannelView) this.view).showErrDialogGetChannel(errCode, 1);
        if (e instanceof HttpException) {

        }
    }

    @Override
    public void onNext(GetChannelsResult result) {
        PortalErrUtil.getInstance().setErrCode("");
        if (result == null || result.getData() == null|| result.getData().size() <= 0) {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB1");
            PortalErrUtil.getInstance().setErrCode(PORTAL_ROOT_COLUMN);
            ((IGetChannelView) this.view).showErrDialogGetChannel(PORTAL_ROOT_COLUMN, 1);
        } else {
            if (this.isLive ) {
                LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fED1");
                PortalErrUtil.getInstance().setErrCode(STR_PRODUCT_WITHOUT_AUTH);
                ((IGetChannelView) this.view).showErrDialogGetChannel(STR_PRODUCT_WITHOUT_AUTH, this.categoryLevel);
            } else {
                ((IGetChannelView) this.view).onSucceedGetChannel(result, this.categoryLevel);
            }
        }
    }
}
